#-------------------------------------------------------------------------------
# Name:        Hunger Games
# Purpose:
#
# Author:      Julia Kettle and Vinicius Mikuni
#
# Created:     16/08/2013
# Copyright:
# Licence:
#-------------------------------------------------------------------------------
#The idea od the strategy is to use, at first, a strategy that keeps the reputation
#close to the average until all the players that only hunts die.
#Since after m rounds, each player presents a number of hunts H; 0 <= H <= m(P-1)
# Two players using different strategies presents a high probability to have a different
# number of hunts, so is for the reputation.
#Using this assumption, all players become distinguishable and will hunt almost
#the same number every round, that way, even though the player_reputations is random
#every round, numpy.sort(player_reputations) will be almost constant.
#Using this idea, a strategy close to the 'tit for tat' was used
import numpy
import random
outcomes_ = list()
first_round = True


def players_choices(food_earnings):
    '''Returns the decision given by the players using the food_earnings'''
    outcomes_ = []
    for result in food_earnings:
        if (result == 3 or result == 0): outcomes_.append('h')
        else: outcomes_.append('s')
    return outcomes_

def hunt_choices(round_number, current_food, current_reputation, m,
            player_reputations):
    reputation_sorted = numpy.argsort(player_reputations)
    global reputation_sorted
    if first_round:
        hunt_decisions = list()
        choices = ['h','s']
        for reputation in player_reputations:
            hunt_decisions.append(random.choice(choices))
        global first_round
        first_round = False

    elif (len(numpy.where(numpy.array(player_reputations) == 1)[0]) > 0 and round_number < 100):
        hunt_decisions = list()
        av_rep = numpy.mean(player_reputations)
        std = numpy.std(player_reputations)
        for reputation in player_reputations:
            if (current_reputation + std < 0.5 and reputation > 0):
                hunt_decisions.append('h')
            elif (current_reputation - std > 0.5):
                hunt_decisions.append('s')
            elif (reputation > (av_rep) ):
                hunt_decisions.append('h')
            else:
                hunt_decisions.append('s')

    else:
            traitor = ['h']
            traitor = traitor * 9
            traitor.append('s')
            hunt_decisions = numpy.zeros((len(player_reputations)),dtype = numpy.str)
            av_rep = numpy.mean(player_reputations)
            tmp = 0
            for tit in numpy.argsort(player_reputations):
                if (0.8<player_reputations[tit] or player_reputations[tit] < 0.4):
                    hunt_decisions[tit] = 's'
                else: hunt_decisions[tit] = sorted_outcomes[tmp]
                if hunt_decisions[tit] == 'h': hunt_decisions[tit] = random.choice(traitor) #small probability to slack instead of cooperate
                tmp = tmp + 1

    return hunt_decisions




def hunt_outcomes(food_earnings):
    outcomes_ = numpy.array(())
    outcomes_ = players_choices(food_earnings)
    sorted_outcomes = numpy.zeros((len(outcomes_)),dtype = numpy.str)
    global sorted_outcomes
    tmp = 0
    for i in reputation_sorted:
        sorted_outcomes[tmp] = outcomes_[i]
        tmp = tmp + 1



def round_end(award, m, number_hunters):
    pass # do nothing


